﻿using System;
using System.Collections;
using System.DirectoryServices;

namespace Framework.IISUtility
{
    public class IISWebServer
    {
        /// <summary>
        ///
        /// </summary>
        internal int index = -1;
        /// <summary>
        ///
        /// </summary>
        public IISWebVirtualDirCollection WebVirtualDirs;
        /// <summary>
        /// 网站说明
        /// </summary>
        public string ServerComment = "Way";
        /// <summary>
        /// 脚本支持
        /// </summary>
        public bool AccessScript = true;
        /// <summary>
        /// 读取
        /// </summary>
        public bool AccessRead = true;
        /// <summary>
        /// 物理路径
        /// </summary>
        public string Path = @"c:\";
        /// <summary>
        /// 端口
        /// </summary>
        public int Port = 80;
        /// <summary>
        /// 目录浏览
        /// </summary>
        public bool EnableDirBrowsing = false;
        /// <summary>
        /// 默认文档
        /// </summary>
        public string DefaultDoc = "index.aspx";
        /// <summary>
        /// 使用默认文档
        /// </summary>
        public bool EnableDefaultDoc = true;
        /// <summary>
        /// IISWebServer的状态
        /// </summary>
        public IISServerState ServerState
        {
            get
            {
                DirectoryEntry server = IISManagement.returnIISWebserver(index);
                if (server == null)
                    throw (new Exception("找不到此IISWebServer"));
                switch (server.Properties["ServerState"][0].ToString())
                {
                    case "2":
                        return IISServerState.Started;
                    case "4":
                        return IISServerState.Stopped;
                    case "6":
                        return IISServerState.Paused;
                }
                return IISServerState.Stopped;
            }
        }


        /// <summary>
        /// 停止IISWebServer
        /// </summary>
        public void Stop()
        {
            if (index == -1)
                throw (new Exception("在IIS找不到此IISWebServer!"));
            try
            {
                var Server = new DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC/" + index);
                Server.Invoke("stop", new object[0]);
            }
            catch
            {
                throw (new Exception("在IIS找不到此IISWebServer!"));
            }
        }


        /// <summary>
        /// 把基本信息的更改更新到IIS
        /// </summary>
        public void CommitChanges()
        {
            IISManagement.EditIISWebServer(this);
        }


        /// <summary>
        /// 启动IISWebServer
        /// </summary>
        public void Start()
        {
            if (index == -1)
                throw (new Exception("在IIS找不到此IISWebServer!"));
            var Service = new DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC");
            DirectoryEntry Server;
            IEnumerator ie = Service.Children.GetEnumerator();
            while (ie.MoveNext())
            {
                Server = (DirectoryEntry)ie.Current;
                if (Server != null && Server.SchemaClassName == "IIsWebServer")
                {
                    if (Server.Properties["Serverbindings"][0].ToString() == ":" + Port + ":")
                    {
                        Server.Invoke("stop", new object[0]);
                    }
                }
            }
            try
            {
                Server = new DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC/" + index);
                Server.Invoke("start", new object[0]);
            }
            catch
            {
                throw (new Exception("在IIS找不到此IISWebServer!"));
            }
        }
        /// <summary>
        ///
        /// </summary>
        public IISWebServer()
        {
            WebVirtualDirs = new IISWebVirtualDirCollection(this);
        }
    }
}
